package com.ruoyi.framework.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqActivity;
import com.ruoyi.wq.domain.WqActivityTheme;
import com.ruoyi.wq.domain.WqActivityUser;
import com.ruoyi.wq.service.IWqActivityService;
import com.ruoyi.wq.service.IWqActivityThemeService;
import com.ruoyi.wq.service.IWqActivityUserService;

import cn.hutool.core.util.RandomUtil;

@Component
public class ActivityService {
	
	@Autowired
	private IWqActivityService wqActivityService;
	
	@Autowired
	private IWqActivityThemeService wqActivityThemeService;
	
	@Autowired
	private IWqActivityUserService wqActivityUserService;
	
	@Autowired
	private ISysNoticeService sysNoticeService;
	
	@Autowired
	private ISysUserService sysUserService;

	public WqActivity saveActivity(WqActivity wqActivity) {
		if(wqActivity.getStartTime() == null || wqActivity.getEndTime() == null){
			throw new ServiceException("请选择预定时间");
		}
		Long userId = SecurityUtils.getUserId();
		SysUser user = sysUserService.getById(userId);
		QueryWrapper<WqActivity> qw = new QueryWrapper<>();
		WqActivity entity = new WqActivity();
		entity.setActivityThemeId(wqActivity.getActivityThemeId());
		entity.setUserId(userId);
		entity.setStatus("0");
		qw.setEntity(entity);
		WqActivity activity = wqActivityService.getOne(qw);
		if(activity == null){
			// 创建活动
			activity = new WqActivity();
			WqActivityTheme theme = wqActivityThemeService.getById(wqActivity.getActivityThemeId());
			if("2".equals(theme.getActivityType())){
				// 单身派对
				if(!"2".equals(user.getIsDog())){
					throw new ServiceException("只有单身可以参加单身派对");
				}
			}
			activity.setStartTime(wqActivity.getStartTime());
			activity.setEndTime(wqActivity.getEndTime());
			activity.setActivityNo(RandomUtil.randomNumbers(5));
			activity.setTitle(theme.getTitle());
			activity.setTag(theme.getTag());
			activity.setPhonenumber(theme.getPhonenumber());
			activity.setAddress(theme.getAddress());
			activity.setLat(theme.getLat());
			activity.setLon(theme.getLon());
			activity.setBanner(theme.getBanner());
			activity.setActivityType(theme.getActivityType());
			activity.setLimitDesc(theme.getLimitDesc());
			activity.setPayPrice(theme.getPayPrice());
			activity.setPrice(theme.getPrice());
			activity.setLimitMan(theme.getLimitMan());
			activity.setSumNum(1L);
			activity.setUserNum(theme.getUserNum());
			activity.setUserId(userId);
			activity.setActivityThemeId(theme.getId());
			
			SysNotice notice = sysNoticeService.selectNoticeById(theme.getNoticeId());
			activity.setContent(notice.getNoticeContent());
			wqActivityService.saveOrUpdate(activity);
			
			// 更新热度
			Long sumNum = wqActivityService.getSumNumByActivityThemeId(theme.getId());
			theme.setSumNum(sumNum);
			wqActivityThemeService.updateById(theme);
			
			// 自动报名
			WqActivityUser activityUser = new WqActivityUser();
			activityUser.setActivityId(activity.getId());
			activityUser.setUserId(userId);
			wqActivityUserService.save(activityUser);
			
			
			Integer gameNum = wqActivityUserService.getSumByUserId(userId);
			user.setGameNum(gameNum);
			sysUserService.updateById(user);
		}
		return activity;
	}

	public synchronized boolean  saveJoinActivity(WqActivity wqActivity) {
		WqActivity activity = wqActivityService.getById(wqActivity.getId());
		if(!"0".equals(activity.getActivityStatus())){
			throw new ServiceException("报名已结束");
		}
		Long userId = SecurityUtils.getUserId();
		QueryWrapper<WqActivityUser> qw = new QueryWrapper<>();
		WqActivityUser entity = new WqActivityUser();
		entity.setActivityId(wqActivity.getId());
		entity.setUserId(userId);
		qw.setEntity(entity);
		WqActivityUser activityUser = wqActivityUserService.getOne(qw);
		if(activityUser == null){
			SysUser user = sysUserService.getById(userId);
			if("0".equals(user.getSex())){
				// 如果是男的
				if(activity.getLimitMan() > 0){
					int manCount = wqActivityUserService.getManUserCount(activity.getId());
					if(manCount >= activity.getLimitMan()){
						throw new ServiceException("男生已满");
					}
				}
			}
			
			if("2".equals(activity.getActivityType())){
				// 单身派对
				if("2".equals(user.getIsDog())){
					throw new ServiceException("只有单身可以参加单身派对");
				}
			}
			
			activityUser = new WqActivityUser();
			activityUser.setActivityId(wqActivity.getId());
			activityUser.setUserId(userId);
			wqActivityUserService.saveOrUpdate(activityUser);
			
			Long sumNum = wqActivityUserService.getSumByActivityId(wqActivity.getId());
			activity.setSumNum(sumNum);
			wqActivityService.updateById(activity);
			
			Integer gameNum = wqActivityUserService.getSumByUserId(userId);
			user.setGameNum(gameNum);
			sysUserService.updateById(user);
		}
		return true;
	}

	public boolean saveOutActivity(WqActivity wqActivity) {
		WqActivity activity = wqActivityService.getById(wqActivity.getId());
		Long userId = SecurityUtils.getUserId();
		if(activity.getUserId().equals(userId)){
			throw new ServiceException("发起者无法取消");
		}
		if(!"0".equals(activity.getActivityStatus())){
			throw new ServiceException("报名已结束，无法取消");
		}
		QueryWrapper<WqActivityUser> qw = new QueryWrapper<>();
		WqActivityUser entity = new WqActivityUser();
		entity.setActivityId(wqActivity.getId());
		entity.setUserId(userId);
		qw.setEntity(entity);
		WqActivityUser activityUser = wqActivityUserService.getOne(qw);
		if(activityUser != null){
			activityUser.setStatus("2");
			wqActivityUserService.updateById(activityUser);
			
			QueryWrapper<WqActivityUser> queryWrapper = new QueryWrapper<>();
			WqActivityUser au = new WqActivityUser();
			au.setActivityId(wqActivity.getId());
			au.setStatus("1");
			queryWrapper.setEntity(au);
			int joinNum = wqActivityUserService.count(queryWrapper);
			activity.setJoinNum(Long.valueOf(joinNum));
			wqActivityService.updateById(activity);
		}
		return true;
	}

	public boolean saveApplyTimeActivity(WqActivity wqActivity) {
		SysUser sysUser = sysUserService.getById(SecurityUtils.getUserId());
		if(!"1".equals(sysUser.getIsManager())){
			throw new ServiceException("权限不足");
		}
		WqActivity activity = wqActivityService.getById(wqActivity.getId());
		if("1".equals(activity.getActivityStatus())){
			activity.setActivityStatus("2");
			activity.setStartTime(wqActivity.getStartTime());
			activity.setEndTime(wqActivity.getEndTime());
			wqActivityService.updateById(activity);
		}
		return true;
	}

	public boolean saveSignTimeActivity(WqActivity wqActivity) {
		SysUser sysUser = sysUserService.getById(SecurityUtils.getUserId());
		if(!"1".equals(sysUser.getIsManager())){
			throw new ServiceException("权限不足");
		}
		WqActivity activity = wqActivityService.getById(wqActivity.getId());
		if("2".equals(activity.getActivityStatus())){
			activity.setActivityStatus("3");
			wqActivityService.updateById(activity);
		}
		return true;
	}
	
}
